﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using wfDesigner.Model;
using System.Windows.Input;
using Microhard.WorkflowDataAccess;

namespace wfDesigner.ViewModel
{
    public class FlowTypeViewModel : INotifyPropertyChanged 
    {
        public FlowTypeViewModel()
        {
          // Query(1);
        }
       
        #region 属性定义
        //记录总数
        public int _total;
        public int Total
        {
            get
            {
                return _total;
            }
            set
            {
                _total = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Total"));
                }
            }
        }

        //第几页
        public int _pageIndex=1;
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PageIndex"));
                }
            }
        }

        //每页显示记录数
        public int _pageSize=10;
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PageSize"));
                }
            }
        }

        
        public int _SelIndex ;
        public int SelIndex
        {
            get
            {
                return _SelIndex;
            }
            set
            {
                _SelIndex = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("SelIndex"));
                }
            }
        }
        public FlowType _selItem;
        public FlowType SelItem
        {
            get
            {
                return _selItem;
            }
            set
            {
                _selItem = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("SelItem"));
                }
            }
        }
        private List<FlowType> _typeList;
        public List<FlowType> TypeList
        {
            get
            {
                return _typeList;
            }
            set
            {
                _typeList = value;
                NotifyPropertyChanged("TypeList");
            }
        } 
        #endregion

        #region 方法定义
       
        private ICommand delete;

        public ICommand Delete
        {
            get
            {
                if (this.delete == null)
                    this.delete = new DelegateCommand(this.ExecuteDelete);
                return this.delete;
            }
        }

        private void ExecuteDelete(object selIndex)
        {
            Guid typeid = TypeList[(int)selIndex].TypeID;
            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            var sel = entity.t_WorkFlowTypes.Where(n => n.TypeId == typeid).SingleOrDefault();
            entity.t_WorkFlowTypes.DeleteOnSubmit(sel);

            entity.SubmitChanges();
            Query(1);
        }
        public void Query(int pageIndex)
        {
            int id = (pageIndex - 1) * _pageSize + 1;
            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            var typeList = entity.t_WorkFlowTypes.Select(n => new { n.TypeId, n.TypeName, n.TypeRemark, n.CreateDate });
            Total = typeList.Count();

            if (Total > 0)
            {
                var total = typeList.OrderBy(n => n.CreateDate).Skip((pageIndex - 1) * _pageSize).Take(_pageSize);
                List<FlowType> list = new List<FlowType>();

                foreach (var t in total)
                {
                    FlowType type = new FlowType();
                    type.ID = id;
                    type.TypeID = t.TypeId;
                    type.TypeName = t.TypeName;
                    type.TypeRemark = t.TypeRemark;
                    type.DateTime =t.CreateDate.ToString().Substring(0,10);

                    list.Add(type);
                    id++;
                }
                this.TypeList = list;
            }
            else
            {
                this.TypeList = null;
            }


        } 
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
